#coding=cp936
import os
import shutil
import win32api
import zipfile

def getFileVersion(file_name):
    info = win32api.GetFileVersionInfo(file_name, os.sep)
    ms = info['FileVersionMS']
    ls = info['FileVersionLS']
    version = '%d.%d.%d.%d' % (win32api.HIWORD(ms), win32api.LOWORD(ms), win32api.HIWORD(ls), win32api.LOWORD(ls))
    return version

def upxit(file_name):
    if not os.path.exists('upxit'):
        os.makedirs('upxit')
    shutil.copy(file_name,'upxit/%s' % file_name)
    os.system('upx -9 upxit/%s' % file_name)
    os.chdir('upxit')
	
    #rarcmd='RAR A GKZFJK_V%s.rar GKZFJK.exe %s' % (getFileVersion(file_name),file_name)
    #os.system(rarcmd)
    #extb=os.path.splitext(file_name)[0]
    
    real_name=os.path.splitext(file_name)[0].upper()
    
    print real_name
	
    zipFile = zipfile.ZipFile(r'%s_V%s.zip' % (real_name,getFileVersion(file_name)), 'w')  
    zipFile.write(file_name, file_name, zipfile.ZIP_DEFLATED)  
    zipFile.close()
	
if __name__ == "__main__":
    #the file_name must be have '.exe' suffix.
    upxit('TODETAIL.exe')






